/**
 *    Copyright 2009-2020 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.aapl.util;

/**
 * @author gu
 * @date 2020/3/27 13:58
 */
public class NameUtil {
  public static String getSetterNameByField(String name) {
    char[] chars = name.toCharArray();
    if (chars == null || chars.length == 0) {
      return "";
    }
    firstCharUpper(chars);
    String m = "set" + new String(chars);
    return m;
  }

  public static String getGetterNameByField(String name) {
    char[] chars = name.toCharArray();
    if (chars == null || chars.length == 0) {
      return "";
    }
    firstCharUpper(chars);
    String m = "get" + new String(chars);
    return m;
  }

  public static void firstCharUpper(char[] chars) {
    if (chars[0] >= 'a' && chars[0] <= 'z') {
      chars[0] = (char) (chars[0] - 32);
    }
  }

  public static String camel2Underline(String name) {
    StringBuilder sb = new StringBuilder();
    char[] chars = name.toCharArray();
    for (char c : chars) {
      if (c >= 'A' && c <= 'Z') {
        c = (char) (c + 0x20);
        sb.append('_');
      }
      sb.append(c);
    }
    return sb.toString();
  }

}
